package servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import domian.Collect;
import domian.PageBean;
import domian.Result;
import domian.User;
import redis.clients.jedis.Jedis;
import service.impl.CollectServiceImpl;
import utils.Redis;
import utils.ToolSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Date;

/**
 * 收藏setvlet
 */
@WebServlet("/getFile/*")
public class GetFileServlet extends BaseServlet{
    CollectServiceImpl collectService = new CollectServiceImpl();
    /**
     * 点击收藏或取消收藏
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void fileId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     //获取请求
        String href = req.getParameter("href");
        String collect = req.getParameter("collect");

       /* System.out.println("地址： "+href);
        System.out.println("收藏信息： "+collect);*/
        //截取地址里的文件的id
        String id = null;
        try {
            URL urlObj = new URL(href);
            String query = urlObj.getQuery();
            String[] params = query.split("&");

            for (String param : params) {
                String[] keyValue = param.split("=");
                String key = URLDecoder.decode(keyValue[0], "UTF-8");
                String value = URLDecoder.decode(keyValue[1], "UTF-8");

                if (key.equals("id")) {
                    id = value;
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //从redis获取当前用户
        Jedis jedis = new Jedis();
        //获取用户客户端标识
        String redisKey = Redis.getRedisKey(req);
        String json = jedis.get(redisKey);
        ObjectMapper om = new ObjectMapper();
        User users = om.readValue(json, User.class);
        Integer uid = users.getUid(); //登录用户id
        String nickname = users.getNickname(); //登录用户昵称
           //调用收藏业务类
           Result result = collectService.CollectMessage(new Collect( uid, Integer.parseInt(id),new Date()),collect,nickname);
           //响应结果
           ToolSet.printWriter(resp,result);
    }

    /**
     * 查看收藏
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void view (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer pages = Integer.valueOf(req.getParameter("pages")); //第几页
        /*String option = req.getParameter("option");//条数*/

        /**
         * 将每页条数提取出来
         */
      /*  String[] parts = option.split("/"); // 将字符串拆分为数组
        String result = parts[1]; // 获取数组中索引为1的元素
        Integer num = Integer.parseInt(result); // 将字符串转换为整数类型*/

       /* System.out.println(pages+"---");*/

        //获取用户客户端标识
        String redisKey = Redis.getRedisKey(req);
        //从redis获取当前用户
        Integer uid = uId(redisKey);
        //将前端数据传入service层
        PageBean pageBean = collectService.viewFavorites(uid, pages);
        //响应结果
        ToolSet.printWriter(resp,pageBean);

    }

    /**
     * 控制收藏按钮 （有收藏显示取消收藏，没有收藏显示收藏）
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void viewFid (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取用户客户端标识
        String redisKey = Redis.getRedisKey(req);
        //从redis获取当前用户
        Integer uid = uId(redisKey);
        PageBean pageBean = collectService.fidCollectService(uid);
        //响应结果
        ToolSet.printWriter(resp,pageBean);

    }
    //封装从redis调用当前登录用户的id方法
    public static Integer uId(String user) throws IOException {
        Jedis jedis = new Jedis();
        String json = jedis.get(user);
        ObjectMapper om = new ObjectMapper();
        User users = om.readValue(json, User.class);
        Integer uid = users.getUid();
        return  uid;
    }
}
